
#load library
install.packages("cjoint")
install.packages("cregg")
library(cjoint)
library(cregg)

#load data
                     C:\Users\petrh\OneDrive\gacr_radikalizace\experiment_cj_tg
conjoint <- read.csv("C://Users//petrh//OneDrive//gacr_radikalizace//experiment_cj_tg//conjoint_data.csv", header = TRUE, sep = ";")
names(conjoint)

head(conjoint)

conjoint$trait_gender <- as.factor(conjoint$trait_gender)
conjoint$trait_education <- as.factor(conjoint$trait_education)
conjoint$trait_value <- as.factor(conjoint$trait_value)
conjoint$trait_party <- as.factor(conjoint$trait_party)
conjoint$trait_immigration <- as.factor(conjoint$trait_immigration)
conjoint$trait_politicaltalk <- as.factor(conjoint$trait_politicaltalk)
conjoint$choice <- as.integer(conjoint$choice)
conjoint$id_ok <- as.numeric(conjoint$id)

conjoint$choice[is.na(conjoint$choice)] <- 99
conjoint <- subset(conjoint, conjoint$choice <= 1)


table(conjoint$Party)
conjoint$Party1[conjoint$Party=="Jinou stranu"] <- NA
conjoint$Party1[conjoint$Party=="��dnou stranu"] <- "��dnou stranu"
conjoint$Party1[conjoint$Party=="ANO"] <- "ANO"
conjoint$Party1[conjoint$Party=="�SSD"] <- "�SSD"
conjoint$Party1[conjoint$Party=="KDU-�SL"] <- "KDU-�SL"
conjoint$Party1[conjoint$Party=="KS�M"] <- "KS�M"
conjoint$Party1[conjoint$Party=="ODS"] <- "ODS"
conjoint$Party1[conjoint$Party=="Pir�ti"] <- "Pir�ti"
conjoint$Party1[conjoint$Party=="SPD"] <- "SPD"
conjoint$Party1[conjoint$Party=="STAN"] <- "STAN"
conjoint$Party1[conjoint$Party=="TOP 09"] <- "TOP 09"

table(conjoint$trait_party)
table(conjoint$trait_party,conjoint$Party)

conjoint$partisanmatch <- "no party match"
conjoint$partisanmatch[conjoint$Party1=="NA"] <- NA
conjoint$partisanmatch[conjoint$Party1=="ANO" & conjoint$trait_party == "ANO"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="�SSD" & conjoint$trait_party == "�SSD"] <-"positive party match"
conjoint$partisanmatch[conjoint$Party1=="KDU-�SL" & conjoint$trait_party == "KDU-�SL"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="KS�M" & conjoint$trait_party == "KS�M"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="ODS" & conjoint$trait_party == "ODS"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="Pir�ti" & conjoint$trait_party == "Pir�ti"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="SPD" & conjoint$trait_party == "SPD"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="STAN" & conjoint$trait_party == "STAN"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="TOP 09" & conjoint$trait_party == "TOP 09"] <- "positive party match"
conjoint$partisanmatch[conjoint$Party1=="��dnou stranu" & conjoint$trait_party == "��dnou"] <- "nonvoter match"
conjoint$partisanmatch[conjoint$NegatParty=="NA"] <- NA
conjoint$partisanmatch[conjoint$NegatParty=="ANO" & conjoint$trait_party == "ANO"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="�SSD" & conjoint$trait_party == "�SSD"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="KDU-�SL" & conjoint$trait_party == "KDU-�SL"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="KS�M" & conjoint$trait_party == "KS�M"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="ODS" & conjoint$trait_party == "ODS"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="Pir�ti" & conjoint$trait_party == "Pir�ti"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="SPD" & conjoint$trait_party == "SPD"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="STAN" & conjoint$trait_party == "STAN"] <- "negative party match"
conjoint$partisanmatch[conjoint$NegatParty=="TOP 09" & conjoint$trait_party == "TOP 09"] <- "negative party match"
conjoint$partisanmatch <- factor(conjoint$partisanmatch, levels = c( "no party match", "nonvoter match", "positive party match", "negative party match" ))
table(conjoint$partisanmatch)


#gender
conjoint$gendermatch <- 0
conjoint$gendermatch[conjoint$Gender=="NA"] <- NA 
conjoint$gendermatch[conjoint$Gender== "Mu�" & conjoint$trait_gender == "Mu�"] <- 1
conjoint$gendermatch[conjoint$Gender== "�ena" & conjoint$trait_gender == "�ena"] <- 1
conjoint$gendermatch <- factor(conjoint$gendermatch, levels = c(0, 1), labels = c("no gender match", "gender match"))

# education
conjoint$educationmatch <- 0
conjoint$educationmatch[conjoint$Education =="NA"] <- NA 
conjoint$educationmatch[conjoint$Education == "Z�kladn�" & conjoint$trait_education == "Z�kladn� vzd�l�n�"] <- 1
conjoint$educationmatch[conjoint$Education == "Vyu�en�, bez maturity" & conjoint$trait_education == "St�edn� �kola bez maturity"] <- 1
conjoint$educationmatch[conjoint$Education == "St�edo�kolsk� s maturitou" & conjoint$trait_education == "St�edn� �kola s maturitou"] <- 1
conjoint$educationmatch[conjoint$Education == "Vysoko�kolsk�" & conjoint$trait_education == "Vysoko�kolsk� titul"] <- 1
conjoint$educationmatch <- factor(conjoint$educationmatch, levels = c(0, 1), labels = c("no education match", "education match"))

# value
head(conjoint)
conjoint$MostValue <- factor(conjoint$MostValue, levels = c(1, 2, 3, 4), 
                             labels = c("Prom��len� nov�ch my�lenek a tvo�ivost, d�lat v�ci origin�ln�m zp�sobem", "P�ekvapen� a vyhled�v�n� nov�ch aktivit. D�lat v �ivot� mnoho r�zn�ch v�c�", 
                                        "Chovat se spo��dan�, vyh�bat se tomu, co ostatn� ozna�uj� za �patn�" , "��t v bezpe�n�m prost�ed� a vyh�bat se v�emu, co by mohlo ohrozit bezpe�nost"))
conjoint$valuesmatch <- "value mismatch"
conjoint$valuesmatch[(conjoint$MostValue =="Prom��len� nov�ch my�lenek a tvo�ivost, d�lat v�ci origin�ln�m zp�sobem"|conjoint$MostValue == "P�ekvapen� a vyhled�v�n� nov�ch aktivit. D�lat v �ivot� mnoho r�zn�ch v�c�") & (conjoint$trait_value == "Prom��len� nov�ch my�lenek a tvo�ivost, d�lat v�ci origin�ln�m zp�sobem" | conjoint$trait_value == "P�ekvapen� a vyhled�v�n� nov�ch aktivit. D�lat v �ivot� mnoho r�zn�ch v�c�")] <- "libertarian values match" 
conjoint$valuesmatch[conjoint$MostValue =="Chovat se spo��dan�, vyh�bat se tomu, co ostatn� ozna�uj� za �patn�" & (conjoint$trait_value == "Chovat se spo��dan�, vyh�bat se tomu, co ostatn� ozna�uj� za �patn�"| conjoint$trait_value == "��t v bezpe�n�m prost�ed� a vyh�bat se v�emu, co by mohlo ohrozit bezpe�nost")] <- "authoritarian values match"                             
conjoint$valuesmatch[conjoint$MostValue =="��t v bezpe�n�m prost�ed� a vyh�bat se v�emu, co by mohlo ohrozit bezpe�nost" & (conjoint$trait_value == "Chovat se spo��dan�, vyh�bat se tomu, co ostatn� ozna�uj� za �patn�"| conjoint$trait_value == "��t v bezpe�n�m prost�ed� a vyh�bat se v�emu, co by mohlo ohrozit bezpe�nost")] <- "authoritarian values match"                             
conjoint$valuesmatch <- factor(conjoint$valuesmatch, levels = c("value mismatch", "libertarian values match", "authoritarian values match" ))
table(conjoint$valuesmatch)

# matched political talk
conjoint$politicaltalkmatch <- "political talk mismatch neutral with others"
conjoint$politicaltalkmatch[conjoint$PolitTalk == "O politice mluv�m jenom ob�as" & conjoint$trait_politicaltalk == "Ob�as"] <- "political talk match sometimes"
conjoint$politicaltalkmatch[conjoint$PolitTalk == "O politice mluv�m �asto" & conjoint$trait_politicaltalk == "�asto"] <- "political talk match often"
conjoint$politicaltalkmatch[conjoint$PolitTalk == "O politice nemluv�m t�m�� nikdy" & conjoint$trait_politicaltalk == "Skoro nikdy"] <- "political talk match never"
conjoint$politicaltalkmatch[conjoint$PolitTalk == "O politice mluv�m �asto" & conjoint$trait_politicaltalk == "Skoro nikdy"] <- "political talk mismatch often-never"
conjoint$politicaltalkmatch[conjoint$PolitTalk == "O politice nemluv�m t�m�� nikdy" & conjoint$trait_politicaltalk == "�asto"] <- "political talk mismatch never-often"
conjoint$politicaltalkmatch <- factor(conjoint$politicaltalkmatch, levels = c("political talk mismatch neutral with others", "political talk match sometimes", "political talk match often",
                                                                    "political talk match never", "political talk mismatch often-never",  "political talk mismatch never-often"))



#mateched migration
conjoint$immigrationmatch <- 0
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace je pro �R p��nosn�. D�ky migraci je na�e zem� lep��m m�stem k �ivotu." & conjoint$trait_immigration == "Imigrace �esk� republice prosp�v�"] <- "immigration match: positive" 
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace je pro �R �kodliv�. Kv�li imigraci je na�e zem� hor��m m�stem k �ivotu." & conjoint$trait_immigration == "Imigrace �esk� republice �kod�"] <- "immigration match: negative" 
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace pro �R nen� ani �kodliv� ani p��nosn�. Na kvalit� �ivota v na�� zemi nic nezm�n�." & conjoint$trait_immigration == "Imigrace nen� pro �esko ani p��nosn� ani �kodliv�"] <- "immigration match: neutral"  
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace je pro �R �kodliv�. Kv�li imigraci je na�e zem� hor��m m�stem k �ivotu." & conjoint$trait_immigration == "Imigrace �esk� republice prosp�v�"] <- "immigration mismatch: negative-positive"   
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace je pro �R p��nosn�. D�ky migraci je na�e zem� lep��m m�stem k �ivotu." & conjoint$trait_immigration == "Imigrace �esk� republice �kod�"] <- "immigration mismatch: positive-negative" 
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace je pro �R �kodliv�. Kv�li imigraci je na�e zem� hor��m m�stem k �ivotu." & conjoint$trait_immigration == "Imigrace nen� pro �esko ani p��nosn� ani �kodliv�"] <- "immigration mismatch: negative-neutral"  
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace je pro �R p��nosn�. D�ky migraci je na�e zem� lep��m m�stem k �ivotu." & conjoint$trait_immigration == "Imigrace nen� pro �esko ani p��nosn� ani �kodliv�"] <- "immigration mismatch: positive-neutral"  
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace pro �R nen� ani �kodliv� ani p��nosn�. Na kvalit� �ivota v na�� zemi nic nezm�n�." & conjoint$trait_immigration == "Imigrace �esk� republice prosp�v�"] <- "immigration mismatch: neutral-positive"  
conjoint$immigrationmatch[conjoint$Immigration =="Imigrace pro �R nen� ani �kodliv� ani p��nosn�. Na kvalit� �ivota v na�� zemi nic nezm�n�." & conjoint$trait_immigration == "Imigrace �esk� republice �kod�"] <- "immigration mismatch: neutral-negative"  
table(conjoint$immigrationmatch)
conjoint$immigrationmatch <- factor(conjoint$immigrationmatch, levels = c( "immigration match: neutral","immigration match: positive", "immigration match: negative",  
                                                                           "immigration mismatch: negative-positive", "immigration mismatch: positive-negative", "immigration mismatch: negative-neutral", 
                                                                           "immigration mismatch: positive-neutral", "immigration mismatch: neutral-positive", "immigration mismatch: neutral-negative"))

write.csv(conjoint, "C://Users//SIMU13//Downloads//conjoint_stata.csv")


summary(conjoint)

f1 <- choice ~ partisanmatch + immigrationmatch + politicaltalkmatch + valuesmatch + gendermatch + educationmatch
d1 <- cj(conjoint, f1, id = ~ id_ok, estimate = "amce")
summary(d1)
d1
plot(d1, vline = 0,   colors = "black")

plot_model(d1, colors = "black")


f2 <- rating ~ partisanmatch + immigrationmatch + politicaltalkmatch + valuesmatch + gendermatch + educationmatch
d2 <- cj(conjoint, f2, id = ~ id_ok, estimate = "amce")
d2
plot(d2, vline = 0)


#H3b: Individuals will prefer to talk less to other people with different attitudes on immigration
#who like to talk about politics compared to those with similar attitudes on immigration. 

#recode attitudes to imigration to two categories - math and mismatch
conjoint$immigrationmatch_bin
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration match: neutral"] <- "match"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration match: positive"] <- "match"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration match: negative"] <- "match"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration mismatch: negative-positive"] <- "mismatch"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration mismatch: positive-negative"] <- "mismatch"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration mismatch: negative-neutral"] <- "mismatch"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration mismatch: positive-neutral"] <- "mismatch"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration mismatch: neutral-positive"] <- "mismatch"
conjoint$immigrationmatch_bin[conjoint$immigrationmatch == "immigration mismatch: neutral-negative"] <- "mismatch"

conjoint$immigrationmatch_talk_interation <- 0
conjoint$immigrationmatch_talk_interation[conjoint$immigrationmatch_bin == "mismatch"   & conjoint$trait_politicaltalk == "�asto"] <- "different attitude and talk a lot"
conjoint$immigrationmatch_talk_interation[conjoint$immigrationmatch_bin == "mismatch"   & conjoint$trait_politicaltalk != "�asto"] <- "different attitude and not talk a lot"
conjoint$immigrationmatch_talk_interation[conjoint$immigrationmatch_bin == "match"   & conjoint$trait_politicaltalk == "�asto"] <- "same attitude and talk a lot"
conjoint$immigrationmatch_talk_interation[conjoint$immigrationmatch_bin == "match"   & conjoint$trait_politicaltalk != "�asto"] <- "same attitude and not talk a lot"
conjoint$immigrationmatch_talk_interation <- factor(conjoint$immigrationmatch_talk_interation, levels = c( "same attitude and not talk a lot", "different attitude and talk a lot", "different attitude and not talk a lot",
                                                                                                            "same attitude and talk a lot"))

table( conjoint$immigrationmatch_talk_interation)



f3 <- rating ~ partisanmatch +   valuesmatch + gendermatch + educationmatch + immigrationmatch_talk_interation
d3 <- cj(conjoint, f3, id = ~ id_ok, estimate = "amce")
d3
plot(d3, vline = 0)
f3 <- choice ~ partisanmatch +   valuesmatch + gendermatch + educationmatch + immigrationmatch_talk_interation
d3 <- cj(conjoint, f3, id = ~ id_ok, estimate = "amce")
d3
plot(d3, vline = 0)


table(conjoint$party_talk_interaction)
conjoint$party_talk_interaction <- "no match"
conjoint$party_talk_interaction[conjoint$partisanmatch == "negative party match" 
                                         & conjoint$trait_politicaltalk == "�asto"] <- "negative and talk a lot"
conjoint$party_talk_interaction[conjoint$partisanmatch == "negative party match" 
                                & conjoint$trait_politicaltalk != "�asto"] <- "negative and not talk a lot"
conjoint$party_talk_interaction[conjoint$partisanmatch == "positive party match" 
                                & conjoint$trait_politicaltalk == "�asto"] <- "positive and talk a lot"
conjoint$party_talk_interaction[conjoint$partisanmatch == "positive party match" 
                                & conjoint$trait_politicaltalk != "�asto"] <- "positive and not talk a lot"
conjoint$party_talk_interaction[(conjoint$partisanmatch == "no party match"  |conjoint$partisanmatch == "nonvoter match" )
                                & conjoint$trait_politicaltalk == "�asto"] <- "no party and talk a lot"
conjoint$party_talk_interaction[(conjoint$partisanmatch == "no party match"  |conjoint$partisanmatch == "nonvoter match" )
                                & conjoint$trait_politicaltalk != "�asto"] <- "no party and not talk a lot"

conjoint$party_talk_interaction <- factor(conjoint$party_talk_interaction, levels = c( "no party and not talk a lot", "negative and talk a lot", "negative and not talk a lot", 
                                                                                      "positive and talk a lot", "positive and not talk a lot",
                                                                                      "no party and talk a lot"))


f4 <- rating ~ immigrationmatch +   valuesmatch + gendermatch + educationmatch  + party_talk_interaction
d4 <- cj(conjoint, f3, id = ~ id_ok, estimate = "amce")
d4
plot(d4, vline = 0)

f5 <- choice ~ immigrationmatch +   valuesmatch + gendermatch + educationmatch  + party_talk_interaction
d5 <- cj(conjoint, f5, id = ~ id_ok, estimate = "amce")
d5
plot(d5, vline = 0)



trustgame <- read.csv("C://Users//SIMU13//Downloads//trust_r.csv", header = TRUE, sep = ";")
names(trustgame)
summary(trustgame)
data <- subset(trustgame, trustgame$Value >= 0)
data$treatment <- "control"
data$treatment[data$imigr_neg_neg == 1] <- "imigration: negative match"
data$treatment[data$imigr_pos_pos == 1] <- "imigration: positive match"
data$treatment[data$imigr_neut_neut == 1] <- "imigration: neutral match"
data$treatment[data$imigr_neg_pos == 1] <- "imigration: negative-positive mismatch"
data$treatment[data$imigr_pos_neg == 1] <- "imigration: positive-negative mismatch"
data$treatment[data$imigr_neg_neut == 1] <- "imigration: negative-neutral mismatch"
data$treatment[data$imigr_pos_neut == 1] <- "imigration: positive-neutral mismatch"
data$treatment[data$imigr_neut_neg == 1] <- "imigration: neutral-negative mismatch"
data$treatment[data$imigr_neut_pos == 1] <- "imigration: neutral-positive mismatch"
data$treatment[data$PartyPositive == 1] <- "party: positive match"
data$treatment[data$PartyNegative == 1] <- "party: negative match"
data$treatment[data$nonvoter == 1] <- "party: nonvoter match"
data$treatment[data$PartyRandom == 1] <- "party: mismatch"
data$treatment[data$PlaceboOutgroup == 1] <- "placebo group: negative match"
data$treatment[data$PlaceboIngroup == 1] <- "placebo group: positive match"

data$treatment <- factor(data$treatment, levels = c("control", "imigration: negative match", "imigration: positive match","imigration: neutral match",
                                                    "imigration: negative-positive mismatch","imigration: positive-negative mismatch", "imigration: negative-neutral mismatch",
                                                    "imigration: positive-neutral mismatch", "imigration: neutral-negative mismatch", "imigration: neutral-positive mismatch",
                                                    "party: positive match","party: negative match", "party: nonvoter match", "party: mismatch", "placebo group: negative match", "placebo group: positive match" ))


f6 <- Value ~  treatment 
d6 <- cj(data, f6, id = ~ ResponseId, estimate = "amce")
d6
plot(d6, vline = 0)
